package com.youth.youthservice.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.youth.ClientEntity.OrderCourse;
import com.youth.commonutils.R;
import com.youth.servicebase.exceptionhandler.GlobalException;
import com.youth.youthservice.entity.EduCourse;
import com.youth.youthservice.entity.chapter.ChapterVo;
import com.youth.youthservice.entity.vo.CourseVo.CourseInfoVo;
import com.youth.youthservice.entity.vo.CourseVo.CourseQuery;
import com.youth.youthservice.entity.vo.CourseVo.CourseQueryVo;
import com.youth.youthservice.entity.vo.CourseVo.CourseWebVo;
import com.youth.youthservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author CM
 * @since 2023-4-27
 */
@Api(description = "课程")
@RestController
@RequestMapping("/eduservice/course")
//使用nginx是需要使用@CrossOrigin解决跨域问题的，现在使用gateway网关替代   /*解决跨域问题*/
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;


    @ApiOperation(value = "根据课程id去查询订单里面的课程信息，用于远程调用")
    @GetMapping("/orderCourse/{courseId}")
    public OrderCourse orderCourse(@PathVariable(value = "courseId") String courseId) {
        return courseService.orderCourse(courseId);
    }

    @ApiOperation(value = "查看课程分页列表")
    @PostMapping("pageCourse/{pageNum}/{pageSize}")
    public R listCourseByPage(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize,
                              @RequestBody(required = false) CourseQuery courseQuery) {
       /* CourseQuery courseQuery = new CourseQuery(subjectParentId,
                subjectId,title,teacherId);*/
        return courseService.listCourseByPage(pageNum, pageSize, courseQuery);
    }

    @ApiOperation(value = "添加课程信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        return courseService.saveCourseInfo(courseInfoVo);
    }

    @ApiOperation(value = "根据id查询课程信息")
    @GetMapping("/getCourseInfo/{id}")
    public R getCourseInfoById(@PathVariable("id") String id) {
        return courseService.getCourseInfoById(id);
    }

    @ApiOperation(value = "更新课程信息")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        return courseService.updateCourseInfo(courseInfoVo);
    }

    @ApiOperation(value = "课程发布界面预览")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable("id") String id) {
        return courseService.getPublishCourseInfo(id);
    }

    @ApiOperation(value = "课程正式发布")
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable("id") String id) {
        UpdateWrapper<EduCourse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                .set("status", "Normal");
        boolean update = courseService.update(courseService.getById(id), updateWrapper);
        if (update) {
            return R.ok();
        }
        throw new GlobalException(20004, "课程发布状态修改失败");
    }

    @ApiOperation("删除课程")
    @PostMapping("deleteCourse/{id}")
    public R delete(@PathVariable("id") String CourseId) {
        return courseService.delete(CourseId);
    }


    @ApiOperation(value = "查询所有的课程信息进行显示-远程huang")
    @GetMapping("/getCourseInfoList")
    public R getCourseInfoList() {
        return courseService.getCourseInfoList();
    }


    @ApiOperation(value = "查询课程详情")
    @GetMapping("/selectCourseDetail/{courseId}")
    public R getCourseDetail(@PathVariable("courseId") String courseId) {
        /*获取课程简介信息和对应课程讲师信息*/
        CourseWebVo courseWebVo = courseService.getDetailById(courseId);
        /*获取当前课程的章节信息*/
        List<ChapterVo> chapterVoList = courseService.getChapterList(courseId);
        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList);
    }

    @ApiOperation(value = "前台分页查询课程列表")
    @PostMapping("selectPageCourse/{pageNum}/{pageSize}")
    public R ListCourse(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize,
                        @RequestBody(required = false) CourseQueryVo courseQuery){
        return courseService.listCourse(pageNum, pageSize, courseQuery);
    }




}

